package com.jiyinit.web.core.activiti.service;

import com.jiyinit.web.core.activiti.entity.TabLeaveBillInfoEntity;
import com.jiyinit.web.core.activiti.enums.LeaveGateWayEnum;
import com.jiyinit.web.core.common.config.PropertyPlaceholderConfigurer;
import org.activiti.engine.delegate.DelegateTask;
import org.activiti.engine.delegate.TaskListener;
import org.springframework.stereotype.Component;
import org.springframework.web.context.ContextLoader;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p></p>
 * <p>提出请假申请  监听
 * <PRE>
 * <BR>	修改记录
 * <BR>-----------------------------------------------
 * <BR>	修改日期			修改人			修改内容
 * </PRE>
 *
 * @author jack zh
 * @version 1.0
 * @date 2018/7/19
 * @since 1.0
 */

public class LvTaskUser3Listener implements TaskListener {


    static String LEAVE_DAYS= (String) PropertyPlaceholderConfigurer.getContextProperty("LEAVE_DAYS");

    static String LEAVE_SPECIAL_ORG= (String) PropertyPlaceholderConfigurer.getContextProperty("LEAVE_SPECIAL_ORG");


    private LeaveBillInfoServiceImpl leaveBillInfoService;

    @Override
    public void notify(DelegateTask delegateTask) {

        leaveBillInfoService=(LeaveBillInfoServiceImpl) ContextLoader.getCurrentWebApplicationContext().getBean("leaveBillInfoServiceImpl");


        String exeId= delegateTask.getExecutionId();  //执行实例id

        TabLeaveBillInfoEntity record=new TabLeaveBillInfoEntity();
        //根据实例id查询请假信息
        record.setExecutionId(exeId);
        List<TabLeaveBillInfoEntity> entityList= leaveBillInfoService.findBySelective(record);
        if(entityList.size()>0) {

            record=entityList.get(0);
            // 设置节点拾取人
            delegateTask.setVariable("leaveUserId",record.getUserId()+"");   //设置全局变量方便后面节点调用
            delegateTask.setAssignee(record.getUserId()+"");
            //设置后续节点参数
            delegateTask.setVariable(LeaveGateWayEnum.gateway_4.getCode(), record.getUserLevel());  //设置身份
            //天数判定
            String gateway11res="false";

            if (record.getLvDay() > Integer.parseInt(LEAVE_DAYS)){
                gateway11res="false";
            }else if(record.getLvDay() == Integer.parseInt(LEAVE_DAYS)){
                if(record.getLvHours() > 0){
                    gateway11res="false";
                }else{
                    gateway11res="true";
                }
            }else{
                gateway11res="true";
            }
            delegateTask.setVariable(LeaveGateWayEnum.gateway_11.getCode(), gateway11res);  //设置身天数

            //设置部门
            String orgsFlag="1";
            String[] orgs=LEAVE_SPECIAL_ORG.split(",");
            for (String org:orgs) {
                if(org.equals(record.getOrgId()))
                {
                    orgsFlag="2";
                    break;
                }
            }
            delegateTask.setVariable(LeaveGateWayEnum.gateway_9.getCode(), orgsFlag);  //设置部门
      }
    }
}
